#ifndef LINEARSPLINE_HPP_
#define LINEARSPLINE_HPP_

#include "SplineBase.hpp"
#include <cstddef>

class LinearSpline: public SplineBase
{
public:
  //construct and deconstruct
  LinearSpline();
  LinearSpline(LinearSpline &&) = delete;
  LinearSpline &operator=(const LinearSpline &) = default;
  LinearSpline &operator=(LinearSpline &&) = delete;
  LinearSpline(const LinearSpline &) = default;

  LinearSpline(vector_type x, vector_type y):SplineBase(x,y){
    setupSplines();
  }

  // compute coeff
  void setupSplines();

  // compute interpolate value
  double interpolate(double x) const;

  // compute derivative
  double derivative(double x) const;

private:
  vector_type a; // a(x-x_i)+y_i
};

inline void LinearSpline::setupSplines(){ 
  for(size_t i = 0;i < xData.size()-1;i++){
    a.push_back((yData[i+1] - yData[i])/(xData[i+1] - xData[i]));
  }  
}

inline double LinearSpline::interpolate(double x) const{
  size_t idx = get_closest_left_idx(x);
  return yData[idx] + a[idx]*(x-xData[idx]);  
}

inline double LinearSpline::derivative(double x) const{
  size_t idx = get_closest_left_idx(x);
  return a[idx]; 
}
#endif